/*
 * media.cpp
 *
 *  Created on: 2017年9月9日
 *      Author: xzl
 */

#include "media.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Util/onceToken.h"
#include "Device/Device.h"
#include "cleaner.h"

using namespace std;
using namespace ZL::DEV;
using namespace ZL::Util;

static recursive_mutex s_mtxMapMedia;
static unordered_map<void *, DevChannel::Ptr> s_mapMedia;


#ifdef __cplusplus
extern "C" {
#endif

//////////////////////////Rtsp media///////////////////////////
API_EXPORT MediaContext CALLTYPE createMedia(const char *appName,const char *mediaName) {
    static onceToken s_token([](){
        cleaner::Instance().push_front([](){
            lock_guard<recursive_mutex> lck(s_mtxMapMedia);
            s_mapMedia.clear();
            DebugL << "clear media" << endl;
        });
    },nullptr);


    DevChannel::Ptr ret(new DevChannel(appName,mediaName));
	lock_guard<recursive_mutex> lck(s_mtxMapMedia);
	s_mapMedia.emplace((void *) (ret.get()), ret);
	return ret.get();
}
API_EXPORT void CALLTYPE releaseMedia(MediaContext ctx) {
	lock_guard<recursive_mutex> lck(s_mtxMapMedia);
	s_mapMedia.erase(ctx);
}

API_EXPORT void CALLTYPE media_initVideo(MediaContext ctx, int width, int height, int frameRate) {
	DevChannel *ptr = (DevChannel *) ctx;
	VideoInfo info;
	info.iFrameRate = frameRate;
	info.iWidth = width;
	info.iHeight = height;
	ptr->initVideo(info);
}
API_EXPORT void CALLTYPE media_initAudio(MediaContext ctx, int channel, int sampleBit, int sampleRate) {
	DevChannel *ptr = (DevChannel *) ctx;
	AudioInfo info;
	info.iSampleRate = sampleRate;
	info.iChannel = channel;
	info.iSampleBit = sampleBit;
	ptr->initAudio(info);
}
API_EXPORT void CALLTYPE media_inputH264(MediaContext ctx, void *data, int len, unsigned long stamp) {
	//TimeTicker();
	DevChannel *ptr = (DevChannel *) ctx;
	ptr->inputH264((char *) data, len, stamp);
}
API_EXPORT void CALLTYPE media_inputAAC(MediaContext ctx, void *data, int len,unsigned long stamp) {
	//TimeTicker();
	DevChannel *ptr = (DevChannel *) ctx;
	ptr->inputAAC((char *) data, len, stamp);
}


#ifdef __cplusplus
}
#endif
